const real_time='https://devapi.qweather.com/v7/weather/now?key=04db12f460fb444abecfb58b3c59e650&location='
const location='https://geoapi.qweather.com/v2/city/lookup?key=04db12f460fb444abecfb58b3c59e650&location='
const threeDays='https://devapi.qweather.com/v7/weather/3d?key=04db12f460fb444abecfb58b3c59e650&location='
Page({
    data:{
        city:'',
        longitude:0,//经度
        latitude:0,//维度
        temp:'',//温度
        windDir:'',//风向
        windScale:'0',//风力
        pressure:'',//气压
        text:'',//天气状况
        icon:'',//当前天气图标
        humidity:'',//湿度
        day1:[],//三天预报：图标，最低温，最高温，白天，夜间天气状况，日期，
        day2:[],
        day3:[],
    },
    search(){
        let latitude=this.data.latitude
        let longitude=this.data.longitude
        let url=`${real_time}${longitude},${latitude}`
        let locaUrl=`${location}${longitude},${latitude}`
        let threedays=`${threeDays}${longitude},${latitude}`
        let that=this
        //获取城市信息
        wx.request({
            url:locaUrl,
            success(e){
                console.log(e)
                let{name}=e.data.location[0]
                that.setData({
                    city:name
                })
            }
        })
        //更新天气信息
        wx.request({
          url,
          success(e){
              console.log(e)
              let{temp,windDir,text,windScale,pressure,humidity,icon}=e.data.now
              windScale=windScale.toString()
            that.setData({
                temp:temp+'℃',windDir,text,windScale,pressure,humidity,icon
            })
          }
        })
        //获取三天预报
        wx.request({
          url:threedays,
          success(e){
              console.log(e)
              let day=[3]
              for(let i=0;i<3;i++){
                let data=e.data.daily[i]
                //去掉日期中的年份
                let date=data.fxDate.slice(5)
                //把白天和夜间相同的天气状况合为一个
                let textDay=data.textDay
                let textNight=data.textNight
                if(textDay===textNight){
                    textNight=''
                }else{
                    textNight='~'+textNight
                }
                 day[i]=[data.iconDay,data.tempMin,data.tempMax,textDay,textNight,date,]
                 console.log(day[i])
              }
              that.setData({
                  day1:day[0],day2:day[1],day3:day[2]
              })
          }
        })
    },
    //获取坐标-经纬度
    getLocation(){
        let that=this
        wx.getLocation({
          success(e){
              let{latitude,longitude}=e
              that.setData({
                  latitude,longitude
              })
          },
          complete(){
            that.search()
          }
        })
    },
    onLoad(){
        this.getLocation()
    },
    //下拉刷新
    onPullDownRefresh(){
        let that=this
        wx.stopPullDownRefresh({
          success(){
              that.getLocation()
          }
        })
    }
})